﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace HuiZe.education.Resource.Controls
{
    /// <summary>
    /// PageManager.xaml 的交互逻辑
    /// </summary>
    public partial class PageManager : UserControl
    {
        public Brush PBackground { get; set; } = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffffff"));
        public CornerRadius PCornerRadius { get; set; } = new CornerRadius(2);
        public Thickness PBorderThickness { get; set; }
        public Brush PBorderBrush { get; set; }

        public int PageCount { get; set; }

        public int ItemSourceCount
        {
            get { return (int)GetValue(ItemSourceCountProperty); }
            set { SetValue(ItemSourceCountProperty, value); }
        }
        public static readonly DependencyProperty ItemSourceCountProperty =
            DependencyProperty.Register("ItemSourceCount", typeof(int), typeof(PageManager), new PropertyMetadata(10));


        public int Counts { get; set; } = 6;
        public int CurrentIndex
        {
            get { return (int)GetValue(CurrentIndexProperty); }
            set { SetValue(CurrentIndexProperty, value); }
        }
        public static readonly DependencyProperty CurrentIndexProperty =
            DependencyProperty.Register("CurrentIndex", typeof(int), typeof(PageManager), new PropertyMetadata(1));

        public Action<int> LoadDataCallback { get; set; }

        public PageManager()
        {
            InitializeComponent();
            Loaded += PageManager_Loaded;
        }

        public void UpdataPages()
        {
            //this.MouseDown += PageManage_MouseDown;
            if (uniformGrid.Children.Count > 0)
                uniformGrid.Children.Clear();
            int index = Counts < ItemSourceCount ? Counts : ItemSourceCount;

            if (index > 0)
            {
                CustomRadioButton radioButton0 = new CustomRadioButton()
                {
                    Template = (ControlTemplate)FindResource("TxtRadioButtonTemplate"),
                    Margin = new Thickness(0),
                    MinWidth = 34,//26,
                    MaxHeight = 26,
                    BorderThickness = new Thickness(0),
                    FontSize = 12,
                    IsChecked = false,
                };
                radioButton0.Content = "首页";
                radioButton0.Index = 1;
                uniformGrid.Children.Add(radioButton0);
                radioButton0.Click += RadioButton_Click;

                CustomRadioButton PreradioButton = new CustomRadioButton()
                {
                    Template = (ControlTemplate)FindResource("TxtRadioButtonTemplate"),
                    Margin = new Thickness(5, 0, 5, 0),// new Thickness(9, 0, 0, 0),
                    MinWidth = 30,// 26,
                    MaxHeight = 28,// 26,
                    BorderThickness = new Thickness(0),
                    FontSize = 12,
                    IsChecked = false,
                };
                PreradioButton.Content = "<";
                PreradioButton.Index = -1;
                uniformGrid.Children.Add(PreradioButton);
                PreradioButton.Click += RadioButton_Click;

                int _start = CurrentIndex > index ? CurrentIndex - index : 0;
                if (Counts >= ItemSourceCount) _start = 0;

                for (int i = 1; i <= index; i++)
                {
                    CustomRadioButton radioButton = new CustomRadioButton()
                    {
                        Template = (ControlTemplate)FindResource("TxtRadioButtonTemplate"),
                        Margin = new Thickness(5, 0, 5, 0),// new Thickness(9, 0, 0, 0),
                        MinWidth = 30,//  26,
                        MaxHeight = 28,// 26,
                        BorderThickness = new Thickness(0),
                        FontSize = 12,
                        IsChecked = i == 1 ? true : false,
                    };

                    radioButton.Index = (i + _start);
                    radioButton.Content = (i + _start).ToString();

                    uniformGrid.Children.Add(radioButton);
                    radioButton.Click += RadioButton_Click;
                }

                CustomRadioButton radioButtonNext = new CustomRadioButton()
                {
                    Template = (ControlTemplate)FindResource("TxtRadioButtonTemplate"),
                    Margin = new Thickness(5, 0, 5, 0),// new Thickness(9, 0, 0, 0),
                    MinWidth = 30,// 26,
                    MaxHeight = 28,// 26,
                    BorderThickness = new Thickness(0),
                    FontSize = 12,
                    IsChecked = false,
                };
                radioButtonNext.Content = ">";
                radioButtonNext.Index = +1;
                uniformGrid.Children.Add(radioButtonNext);
                radioButtonNext.Click += RadioButton_Click;

                CustomRadioButton radioButtonLast = new CustomRadioButton()
                {
                    Template = (ControlTemplate)FindResource("TxtRadioButtonTemplate"),
                    Margin = new Thickness(5, 0, 5, 0),//new Thickness(9, 0, 0, 0),
                    MinWidth = 34,//26,
                    MaxHeight = 26,
                    BorderThickness = new Thickness(0),
                    FontSize = 12,
                    IsChecked = false,
                };
                radioButtonLast.Content = "尾页";
                radioButtonLast.Index = ItemSourceCount;
                uniformGrid.Children.Add(radioButtonLast);
                radioButtonLast.Click += RadioButton_Click;
            }
        }

        void PageManager_Loaded(object sender, RoutedEventArgs e)
        {
            UpdataPages();
        }

        public void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            if (_is_index_setting) return;

            bool _update = false;
            string[] _headers = new string[]
            {
                ">","<","首页","尾页"
            };
            int _index_cache = CurrentIndex;
            CustomRadioButton customRadioButton = sender as CustomRadioButton;
            if (customRadioButton.Content.ToString() == ">")
            {
                if (CurrentIndex < ItemSourceCount)
                {
                    CurrentIndex++;
                    _update = true;
                }
            }
            else if (customRadioButton.Content.ToString() == "<")
            {
                if (CurrentIndex > 1)
                {
                    CurrentIndex--;
                    _update = true;
                }
            }
            else if (customRadioButton.Content.ToString() == "首页")
            {
                _update = true;
                CurrentIndex = 1;
            }
            else if (customRadioButton.Content.ToString() == "尾页")
            {
                _update = true;
                CurrentIndex = ItemSourceCount;
            }
            else
            {
                CurrentIndex = customRadioButton.Index;
            }

            if (_update)
            {
                UpdataPages();
            }

            _is_index_setting = true;
            foreach (CustomRadioButton _crb in uniformGrid.Children)
            {
                _crb.IsChecked = new bool?(!_headers.Contains(_crb.Content.ToString()) && CurrentIndex == _crb.Index);
            }
            _is_index_setting = false;

            if (_index_cache != CurrentIndex && LoadDataCallback != null)
                LoadDataCallback(CurrentIndex);
        }


        bool _is_index_setting;
    }
}
